<?php
class Weixin_menuAction extends CommonAction {
	public function index(){
		$button=$this->dao->where(array('pid'=>0,'site_id' =>$this->siteid))->order('sort')->select();
		foreach($button as $k=>$v){
			$button[$k]['sub_button']=$this->dao->where(array('pid'=>$v['id']))->order('sort')->select();
		}
		$this->assign('list',$button);
		$this->display();
	}
	public function add(){
		$this->pid_list=$this->dao->where(array('pid'=>0,'site_id' =>$this->siteid))->select();
		parent::add();
	}
	public function edit(){
		$this->pid_list=$this->dao->where(array('pid'=>0,'site_id' =>$this->siteid))->select();
		parent::edit();
	}
	
	function indextree(){
		$pid = $this->_get('pid') ? $this->_get('pid') : 0;
		$this->listmap.=" and pid=".$pid;
		$this->listorder = 'sort';
		if($this->_param('keywords')){
			$keywords=$this->_param('keywords');
			$_GET['keywords']=urlencode($this->_param('keywords'));
			$this->listmap.=" and (title like '%{$keywords}%' or name like '%{$keywords}%')";
			$this->keywords=$keywords;
		}
		
		$this->initView();
        parent::_list();
		$list=$this->list;
		$newsdb = M('News');
		foreach($list as $k=>$v){
			$list[$k]['atriclecount'] = $newsdb -> where(array('category_id' => $v['id'], 'site_id' => $this->siteid)) -> count();
		}
		$this->assign('list',$list);
		$str = $this->fetch();
		$list = $this->view -> get('list');
		foreach($list as $k => $v){
			$str = str_replace('<!--sub#'.$v['id'].'-->', $this->treehtml($v['id']), $str);
		}
		
		$list = $_GET['list'];
		$empty = 1;
		$statu=$list?1:0;
		if($this->get('list')){
			$empty = 0;
		}
		
		die(json_encode(array('empty' => $empty,'tbody'=>$str,'dump'=>$_REQUEST,'statu'=>$statu, 'pages' => $this-> view -> get('pages'))));
	}
	private function treehtml($pid = 0){
		$list = $this->dao -> where(array('site_id' => __SITEID__, 'pid' => $pid)) -> order('sort') -> select();
		if($list){
			$newsdb = M('News');
			foreach($list as $k=>$v){
				$list[$k]['atriclecount'] = $newsdb -> where(array('category_id' => $v['id'], 'site_id' => $this->siteid)) -> count();
			}
			$this->assign('list', $list);
			$curstr = $this->fetch('indextree');
			foreach($this->list as $k => $v){
			    $curstr = str_replace('<!--sub#'.$v['id'].'-->', $this->treehtml($v['id']), $curstr);
		    }
			return $curstr;
		}
	}
	 
	private function parsesortdata($arr = array(), $ret = array(), $pid = 0){
		$pid1 = $pid;
		foreach($arr as $k => $v){
			$p['pid'] = $pid1;
			$p['id'] = $v['id'];
			$ret[] = $p;
			if(isset($v['children'])){
				$pid = $v['id'];
				$ret = $this->parsesortdata($v['children'], $ret, $pid);
			}
		}
		return $ret;
	}
	
	public function updatesort($data){
		$data = $this->parsesortdata(json_decode($_POST['data'], true));
		foreach($data as $k => $v){
			$map['id'] = $v['id'];
			$map['site_id'] = $this->siteid;
			$data['sort'] = $k + 1;
			$data['pid'] = $v['pid'];
			$this->dao -> where($map) -> save($data);
		}
	}
	public function update(){
		if($this->_post('type') == 'view' && !check_url($this->_post('key_or_url'))){
			$this->error('URL不合法！');exit;
		}
		
		if($this->_post('type') == 'click' && $this->_post('key_or_url') == ''){
			$this->error('关键词不能为空！');exit;
		}
		//只能加三个菜单
		/*$pc = $this->dao -> where(array('pid' => 0, 'site_id' => $this->siteid)) -> count();
		if($pc >= 3 && $this->_post('pid') == 0){
			$this->error('一级菜单只能添加3个！');exit;
		}*/
		
		parent::update();
	}
	public function insert(){
		if($this->_post('type') == 'view' && !check_url($this->_post('key_or_url'))){
			$this->error('URL不合法！');exit;
		}
		
		if($this->_post('type') == 'click' && $this->_post('key_or_url') == ''){
			$this->error('关键词不能为空！');exit;
		}
		//只能加三个菜单
		$pc = $this->dao -> where(array('pid' => 0, 'site_id' => $this->siteid)) -> count();
		if($pc >= 3 && $this->_post('pid') == 0){
			$this->error('一级菜单只能添加3个！');exit;
		}
		if($this->_post('pid') > 0){
		    $pc = $this->dao -> where(array('pid' => $this->_post('pid'), 'site_id' => $this->siteid)) -> count();
			if($pc >= 5){
				$this->error('二级菜单只能添加5个！');exit;
			}
		}
		parent::insert();
	}
	
	
	
	public function selectMenu(){
		Vendor('Weixin.Weixin');
		if(!defined('__SITEID__')){ define('__SITEID__',$this->siteid);}
		$d['token']=getSysVal('token');
        $d['appid']=getSysVal('appid');
        $d['appsecret']=getSysVal('appsecret');
		//dump($d);exit();
		$this->weixin=new WeixinChat($d);
		dump($this->weixin->getMenu());
	}
	public function saveMenu(){
		$site = M('Site') -> where(array('id' => $this->siteid)) -> find();
		if($site['relate_id'] > 0){
			$this->success('保存失败了，当前不是主站，无法发布菜单。您可以脱离关联站点后再发布。');
			exit;
		}
		Vendor('Weixin.Weixin');
		if(!defined('__SITEID__')){ define('__SITEID__',$this->siteid);}
		$d['token']=getSysVal('token');
        $d['appid']=getSysVal('appid');
        $d['appsecret']=getSysVal('appsecret');
		//dump($d);exit();
		$this->weixin=new WeixinChat($d);
		$data=$this->dao->where(array('pid'=>0,'site_id'=>$this->siteid))->order('sort')->select();
		foreach($data as $k=>$v){
			$sub_button=$this->dao->where(array('pid'=>$v['id'],'site_id'=>$this->siteid))->order('sort')->select();
			$data[$k]['sub_button']=$sub_button;
		}
		$menu=array();
		foreach($data as $k=>$v){
			$menu['button'][$k]['name']=$v['name'];
			if(!is_array($v['sub_button'])){
				$menu['button'][$k]['type']=$v['type'];
				if($v['type']=='click'){
					$menu['button'][$k]['key']=$v['key_or_url'];
				}else{
					$menu['button'][$k]['url']=$v['key_or_url'];
				}
			}else{
				foreach($v['sub_button'] as $kk=>$vv){
					$menu['button'][$k]['sub_button'][$kk]['type']=$vv['type'];
					$menu['button'][$k]['sub_button'][$kk]['name']=$vv['name'];
					if($vv['type']=='click'){
						$menu['button'][$k]['sub_button'][$kk]['key']=$vv['key_or_url'];
					}else{
						$menu['button'][$k]['sub_button'][$kk]['url']=$vv['key_or_url'];
					}
				}
			}
		}
		
		
		if($this->weixin->createMenu($menu)){
			$this->success('成功保存,菜单更新后将在24小时内生效');
		}else{
			$this->success('保存失败了'.$this->weixin -> errMsg);
		}
	}
	
	//删除自定义菜单
	public function delWeixinMenu(){
		$site = M('Site') -> where(array('id' => $this->siteid)) -> find();
		if($site['relate_id'] > 0){
			$this->success('删除失败了，当前不是主站，无法删除菜单。您可以脱离关联站点后再删除。');
			exit;
		}
		Vendor('Weixin.Weixin');
		if(!defined('__SITEID__')){ define('__SITEID__',$this->siteid);}
		$d['token']=getSysVal('token');
        $d['appid']=getSysVal('appid');
        $d['appsecret']=getSysVal('appsecret');
		$this->weixin=new WeixinChat($d);
		if($this->weixin->deleteMenu()){
			$this->success('成功删除,菜单更新后将在24小时内生效');
		}else{
			$this->success('保存失败了,请检查您的微信接口配置是否正确');
		}
	}
}
